package sk.mucha.tracking.classes;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.IValidator;
import org.apache.wicket.validation.ValidationError;

import sk.mucha.tracking.WicketApplication;

public class AjaxEmailValidator implements IValidator<String> {

	public AjaxEmailValidator() {
	
	}
	
	public void validate(IValidatable<String> validatable) {
		List<User> users = ((WicketApplication)WebApplication.get()).getUsers();
		final String value = validatable.getValue();
		for (User u:users) {
			if (u.getUserEmail().equalsIgnoreCase(value)) {
				ValidationError error = new ValidationError();
				error.setMessage("Email \"" + value + "\" already exists.");
				validatable.error(error);
			}
		}
	}

}
